/*
 * MainFrame.java
 *
 * Created on 21. Oktober 2008, 11:35
 */

package testquicktime2;

import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.Timer;
import quicktime.QTException;
import quicktime.QTSession;
import quicktime.app.view.QTComponent;
import quicktime.app.view.QTFactory;
import quicktime.std.StdQTConstants;
import quicktime.std.StdQTException;
import quicktime.std.movies.Movie;
import quicktime.std.movies.MovieController;
import quicktime.std.movies.media.DataRef;

/**
 *
 * @author  micc
 */
public class MainFrame extends javax.swing.JFrame {
    
    private Movie movie = null;
    private JMenu menuTime = null;

    /** Creates new form MainFrame */
    public MainFrame() {
        // initComponents();
        super ("QT-URL");
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        initMenu();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }

    public void openUrl(String url) {
        try {

            QTSession.open();

            DataRef dr = new DataRef(url);
            movie = Movie.fromDataRef(dr, StdQTConstants.newMovieActive);
            MovieController mc = new MovieController(movie);

            QTComponent qc = QTFactory.makeQTComponent(movie);
            qc.setMovieController(mc);
            Component c = qc.asComponent();

            Container content = this.getContentPane();
            content.add(c);
           
            movie.prePreroll(0, 1.0f);
            movie.preroll(0, 1.0f);
            // movie.start();
            
            this.setSize(400, 300);

        } catch (QTException ex) {
            ex.printStackTrace();
        }
    }

    private void initMenu() {
        
        JMenuBar menuBar = new JMenuBar();
        this.setJMenuBar(menuBar);
        
        // file
        JMenu menuFile = new JMenu("File");
        menuBar.add(menuFile);

        menuFile.add(new JMenuItem(new OpenUrlAction()));
        menuFile.add(new JMenuItem(new QuitAction()));
        
        // movie
        JMenu menuMovie = new JMenu("Movie");
        menuBar.add(menuMovie);
        
        menuMovie.add(new JMenuItem(new StartAction()));
        menuMovie.add(new JMenuItem(new StopAction()));
        
        // time
        menuTime = new JMenu("-:--");
        menuBar.add(menuTime);

        pack();

    }
    
    
    public class OpenUrlAction extends AbstractAction {

        public OpenUrlAction() {
            super("Open URL");
            putValue(Action.SHORT_DESCRIPTION, "Open URL");
            putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
        }

        public void actionPerformed(ActionEvent e) {
            String url = "mms://schweizer-8.wmod.llnwd.net/a1480/e1/vod/glanzundgloria/2008/10/glanzundgloria_20081015_184531_1500k.wmv";
            url = javax.swing.JOptionPane.showInputDialog(this, url);
            openUrl(url);
        }
    }
    
    public class QuitAction extends AbstractAction {

        public QuitAction() {
            super("Quit");
            putValue(Action.SHORT_DESCRIPTION, "Quit");
            putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_Q));
        }

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
    
    public class StartAction extends AbstractAction {

        private Timer timer = null;
        
        public StartAction() {
            super("Start");
            putValue(Action.SHORT_DESCRIPTION, "Start");
            putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_1));
                    
            timer = new Timer(250,this);
            timer.start();
        }

        public void actionPerformed(ActionEvent e) {
            
            if (movie == null) {
                return;
            }

            try {
                movie.start();

                int totalSeconds = movie.getTime() / movie.getTimeScale();
                int seconds = totalSeconds % 60;
                int minutes = totalSeconds / 60;
                String secString = (seconds > 9) ? Integer.toString(seconds) : ("0" + Integer.toString(seconds));
                String minString = Integer.toString(minutes);

                menuTime.setText(minString + ":" + secString);
                
            } catch (StdQTException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
 
        public class StopAction extends AbstractAction {

        public StopAction() {
            super("Stop");
            putValue(Action.SHORT_DESCRIPTION, "Stop");
            putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_2));
        }

        public void actionPerformed(ActionEvent e) {
            try {
                movie.stop();
            } catch (StdQTException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}

